/*
 * Copyright 2011 Marcin Blazejewski
 *
 * This file is part of Ground0
 *
 * Ground0 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ground0 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ground0. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifndef PROPERTIES_H_
#define PROPERTIES_H_

#include <map>
#include <string>
#include <string.h>

using namespace std;

struct ltstr {
    bool operator()(const char* s1, const char* s2) const {
        return strcmp(s1, s2) < 0;
    }
};

string lower(string& s);


/** String based key-value properties
 *  Holds a set of key-value pairs. Can be loaded from a file. 
 *  Useful for simple configuration loading. Supports int and float
 *  conversions and get methods with default values.
 */
class Properties {
private:
    map<string, string>   data;

public:
    Properties();
    Properties(const char *filename);

    void              read_file(const char *filename);
    map<string, string>& get_data() { return data; }

    string            get_str(const char *key, const char *defval);
    const string&     get_str(const char *key);
    int               get_int(const char *key, const int defval);
    int               get_int(const char *key);
    float             get_float(const char *key, const float defval);
    float             get_float(const char *key);

    const string&     set(const char *key, const char *value);
};



/** Filtering property cursor to get a set of properties.
 *  Initialize it with a property key prefix and iterate:<br>
 *  Cursor cursor(props, "my_key.");<br>
 *  while (cursor.has_next) { ... cursor.get_next() ... }<br>
 *  This will iterate through all properties named "my_key.*"
 */
class PropertyCursor {
private:
    map<string, string> &src;
    const char       *prefix;
    int               prefix_len;

    map<string,string>::iterator i;

public:
    PropertyCursor(Properties &src, const char *prefix);
    
    void              reset();
    bool              has_next();
    const string&     get_next();
};

#endif

